package controllers

import (
	"2301a_qidian/cache"
	"2301a_qidian/models"
	"context"
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
)

var Ctx = context.Background()

type NovelController struct {
	beego.Controller
}

// msg
type Response struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

// 小说添加add
func (c *NovelController) Add() {
	//接收信息
	title := c.GetString("title")
	author := c.GetString("author")
	content := c.GetString("content")

	//验证不能为空
	if title == "" || author == "" || content == "" {
		res := Response{
			Code: 500,
			Msg:  "参数错误",
			Data: nil,
		}
		c.Data["json"] = res
		c.ServeJSON()
		return
	}

	//接收 novle结构体
	var novel models.Novel
	novel.Title = title
	novel.Author = author
	novel.Content = content

	//调用方法
	if novel.Add() != nil {
		res := Response{
			Code: 500,
			Msg:  "添加失败",
			Data: nil,
		}
		c.Data["json"] = res
		c.ServeJSON()
		return
	}

	novels, _ := novel.List()
	jsonData, _ := json.Marshal(novels)
	//更新redis缓存
	cache.Rdb.Set(cache.Ctx, "novel_add", jsonData, 0)

	res := Response{
		Code: 200,
		Msg:  "添加成功",
		Data: nil,
	}
	c.Data["json"] = res
	c.ServeJSON()
	return

}

// 小说列表list
func (c *NovelController) List() {
	//获取novel数据
	cacheData := cache.Rdb.Get(cache.Ctx, "novel_list").Val()
	//接收novel结构体
	var novels []*models.Novel
	if cacheData == "" {
		models.DB.Find(&novels)
		jsonData, _ := json.Marshal(novels)
		cache.Rdb.Set(cache.Ctx, "novel_list", jsonData, 0)
	} else {
		//反序列化
		json.Unmarshal([]byte(cacheData), &novels)
	}

	res := Response{
		Code: 200,
		Msg:  "获取成功",
		Data: novels,
	}
	c.Data["json"] = res
	c.ServeJSON()
	return
}

// 删除
func (c *NovelController) Del() {
	//接收id
	id, _ := c.GetInt("id")
	//验证id不为空
	if id < 1 {
		res := Response{
			Code: 500,
			Msg:  "参数不正确",
			Data: nil,
		}
		c.Data["json"] = res
		c.ServeJSON()
		return
	}

	var novel models.Novel
	novel.ID = uint(0)

	if novel.Del() != nil {
		res := Response{
			Code: 500,
			Msg:  "删除失败",
			Data: nil,
		}
		c.Data["json"] = res
		c.ServeJSON()
		return
	}

	novels, _ := novel.List()
	jsonData, _ := json.Marshal(novels)
	//更新redis缓存
	cache.Rdb.Set(cache.Ctx, "novel_del", jsonData, 0)

	res := Response{
		Code: 200,
		Msg:  "删除成功",
		Data: nil,
	}
	c.Data["json"] = res
	c.ServeJSON()
	return

}

// 修改
func (c *NovelController) Update() {
	//接收id
	id, _ := c.GetInt("id")
	//验证id不为空
	if id < 1 {
		res := Response{
			Code: 500,
			Msg:  "参数不正确",
			Data: nil,
		}
		c.Data["json"] = res
		c.ServeJSON()
		return
	}

	//接收novel结构体
	var novel models.Novel
	//实例化
	novel.ID = uint(id)
	novel.Title = c.GetString("title")
	novel.Author = c.GetString("author")
	novel.Content = c.GetString("content")

	//调用方法
	if novel.Update() != nil {
		res := Response{
			Code: 500,
			Msg:  "更新失败",
			Data: nil,
		}
		c.Data["json"] = res
		c.ServeJSON()
		return
	}

	novels, _ := novel.List()
	jsonData, _ := json.Marshal(novels)
	//更新redis缓存
	cache.Rdb.Set(cache.Ctx, "novel_update", jsonData, 0)

	res := Response{
		Code: 200,
		Msg:  "更新成功",
		Data: nil,
	}
	c.Data["json"] = res
	c.ServeJSON()
	return

}

// 获取小说详情detail
func (c *NovelController) Detail() {
	//接收id
	id, _ := c.GetInt("id")
	//验证id不为空
	if id < 1 {
		res := Response{
			Code: 500,
			Msg:  "参数不正确",
			Data: nil,
		}
		c.Data["json"] = res
		c.ServeJSON()
		return
	}

	var novel models.Novel
	novel.ID = uint(0)

	if novel.Detail() != nil {
		res := Response{
			Code: 500,
			Msg:  "获取失败",
			Data: nil,
		}
		c.Data["json"] = res
		c.ServeJSON()
		return
	}

	res := Response{
		Code: 200,
		Msg:  "获取成功",
		Data: novel,
	}
	c.Data["json"] = res
	c.ServeJSON()
	return

}

// 审核文章
func (c *NovelController) UpdateStatus() {
	//接收id
	id, _ := c.GetInt("id")
	//验证id不为空
	if id < 1 {
		res := Response{
			Code: 500,
			Msg:  "参数不正确",
			Data: nil,
		}
		c.Data["json"] = res
		c.ServeJSON()
		return
	}

	status, _ := c.GetInt("status")
	if status != 0 || status != 1 {
		res := Response{
			Code: 500,
			Msg:  "参数有误",
			Data: nil,
		}
		c.Data["json"] = res
		c.ServeJSON()
		return
	}

	var novel models.Novel
	novel.ID = uint(id)
	novel.Status = status
	if novel.Update() != nil {
		res := Response{
			Code: 500,
			Msg:  "更新失败",
			Data: nil,
		}
		c.Data["json"] = res
		c.ServeJSON()
		return
	}

	res := Response{
		Code: 200,
		Msg:  "更新成功",
		Data: novel,
	}
	c.Data["json"] = res
	c.ServeJSON()
	return

}
